//
//  SunGesLoginVC.m
//  Xspcf
//
//  Created by sunshine on 16/11/17.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunGesLoginVC.h"
#import "PCCircleView.h"
#import "PCCircleViewConst.h"
#import "PCLockLabel.h"
#import "PCCircleInfoView.h"
#import "PCCircle.h"

#import "AppDelegate.h"
#import "SunNavController.h"
#import "SunLoginVC.h"
#import "SunLoginCheckVC.h"

#undef Y
#define Y(y) NY(y)

@interface SunGesLoginVC ()<CircleViewDelegate>
{
    int _time;
    PCLockLabel *_msgLabel;
    PCCircleView *_lockView;
}
@end

@implementation SunGesLoginVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear: animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BLUE;
    _time = 5;
    [self creatUI];
}

- (void)creatUI{
    UIImageView *img = [[UIImageView alloc]init];
    [self.view addSubview:img];
    [img lt_setSize:CGSizeMake(X(125), Y(125)) save:YES];
    [img lt_XcenterTo:nil save:YES];
    [img lt_setTop:Y(35) to:nil save:YES];
    img.image = [UIImage imageNamed:@"login_head"];
    UILabel *label = [[UILabel alloc]init];
    [self.view addSubview:label];
    [label lt_XcenterTo:nil save:YES];
    [label lt_setTop:Y(18) to:img save:YES];
    label.textColor = WHITE;
    label.font = FONT(16);
    NSString *phone = [Sun getUser:PHONE];
    label.text = [NSString stringWithFormat:@"%@****%@,欢迎回来!",[phone substringToIndex:3],[phone substringFromIndex:7]];
    _msgLabel = [[PCLockLabel alloc]init];
    [self.view addSubview:_msgLabel];
    [_msgLabel lt_XcenterTo:nil save:YES];
    [_msgLabel lt_setTop:Y(6) to:label save:YES];
    _msgLabel.isLogin = YES;
    [_msgLabel showNormalMsg:@"请输入手势密码"];
    _lockView = [[PCCircleView alloc] initWithType:CircleViewTypeLogin];
    _lockView.delegate = self;
    [self.view addSubview:_lockView];
    [_lockView lt_XcenterTo:nil save:YES];
    [_lockView lt_setTop:Y(24) to:_msgLabel save:YES];
    [_lockView lt_setSize:CGSizeMake(X(270), X(270)) save:YES];
    _lockView.type = CircleViewTypeLogin;
    UIView *line = [[UIView alloc]init];
    [self.view addSubview:line];
    line.backgroundColor = WHITE;
    [line lt_setSize:CGSizeMake(X(1), Y(16)) save:YES];
    [line lt_XcenterTo:nil save:YES];
    [line lt_setTop:Y(50) to:_lockView save:YES];
    UIButton *btn1 = [[UIButton alloc]init];
    [self.view addSubview:btn1];
    [btn1 setTitleColor:WHITE forState:UIControlStateNormal];
    btn1.titleLabel.font = FONT(16);
    [btn1 setTitle:@"其他账户登录" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(otherUser) forControlEvents:UIControlEventTouchUpInside];
    [btn1 lt_YcenterTo:line save:YES];
    [btn1 lt_layoutView:btn1 side:SunRight to:line toSide:SunLeft withSize:1 offset:-X(15) save:YES];
    UIButton *btn2 = [[UIButton alloc]init];
    [self.view addSubview:btn2];
    [btn2 setTitleColor:WHITE forState:UIControlStateNormal];
    btn2.titleLabel.font = FONT(16);
    [btn2 setTitle:@"忘记手势密码" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(useCode) forControlEvents:UIControlEventTouchUpInside];
    [btn2 lt_YcenterTo:line save:YES];
    [btn2 lt_layoutView:btn2 side:SunLeft to:line toSide:SunRight withSize:1 offset:X(15) save:YES];
    
}

- (void)otherUser{
    SunLoginVC *vc = [[SunLoginVC alloc]init];
    vc.isLogin = YES;
    vc.isWeb = NO;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)useCode{
    SunLoginCheckVC *vc = [[SunLoginCheckVC alloc]init];
    vc.isLogin = YES;
    vc.phone = [[[NSUserDefaults standardUserDefaults]objectForKey:@"USER_INFO"]objectForKey:@"username"];
    [self.navigationController pushViewController:vc animated:YES];
}




#pragma mark - circleView - delegate
- (void)circleView:(PCCircleView *)view type:(CircleViewType)type didCompleteLoginGesture:(NSString *)gesture result:(BOOL)equal
{
    if (equal) {
        id temp = [UIApplication sharedApplication].delegate;
        AppDelegate *delegate = temp;
        delegate.tab = [[SunTabController alloc]init];
        delegate.window.rootViewController = delegate.tab;
    } else {
        _time--;
        if (_time <= 0) {
            _time = 0;
            [_msgLabel showWarnMsgAndShake:[NSString stringWithFormat:@"密码错误，还剩%d次机会",_time]];
            SunLoginCheckVC *vc = [[SunLoginCheckVC alloc]init];
            vc.isLogin = YES;
            vc.phone = [[[NSUserDefaults standardUserDefaults]objectForKey:@"USER_INFO"]objectForKey:@"username"];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [_msgLabel showWarnMsgAndShake:[NSString stringWithFormat:@"密码错误，还剩%d次机会",_time]];
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
